package jp.eisbahn.mgapi.api.voice;

import jp.eisbahn.mgapi.api.Response;
import jp.eisbahn.mgapi.http.HttpResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

/**
 * This class has an ability to change the response to the model object.
 * @author Yoichiro Tanaka
 */
public class VoiceUserResponse extends Response {

    /*
     * (non-Javadoc)
     * @see jp.eisbahn.mgapi.api.Response#parseResponse(
     * jp.eisbahn.mgapi.http.HttpResponse)
     */
    @Override
    public final void parseResponse(final HttpResponse httpResponse) {
        String responseBody = httpResponse.getResponseBody();
        Object parsed = JSONValue.parse(responseBody);
        if (parsed instanceof JSONArray) {
            JSONArray entries = (JSONArray) parsed;
            for (Object entryObj : entries) {
                JSONObject entry = (JSONObject) entryObj;
                addEntry(new User(entry));
            }
        } else {
            throw new IllegalStateException(
                    "Response is not JSON object. " + responseBody);
        }
    }

}
